<?php
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition End User License Agreement
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magento.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
 * @license http://www.magento.com/license/enterprise-edition
 */

/**
 * Adminhtml customer grid block
 *
 * @category   Mage
 * @package    Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Mage_Adminhtml_Block_Customer_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

    public function __construct()
    {
        parent::__construct();
        $this->setId('customerGrid');
        $this->setUseAjax(true);
        $this->setDefaultSort('entity_id');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
        //获取当前站点Id 
        $websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->addAttributeToSelect('mobile')
            ->addAttributeToSelect('confirmation')
            ->addNameToSelect()
            ->joinAttribute('billing_id', 'customer_address/entity_id', 'default_billing', null, 'left')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
            if($websiteId){
               $collection->addAttributeToFilter('website_id',$websiteId);
            }
        //$select = $collection->getSelect();
        //$select->joinLeft(array('oe' => $collection->getTable('sales/order')), 'oe.customer_id=e.entity_id', array('orders'=>'count(oe.entity_id)'));
        //$select->joinLeft(array('oe' => $collection->getTable('sales/order')), 'oe.customer_id=e.entity_id', array('order_total'=>'sum(oe.entity_id)'));
        //$select->group('e.entity_id');
        //echo $select;
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn('entity_id', array(
            'header'    => Mage::helper('customer')->__('ID'),
            'width'     => '50px',
            'index'     => 'entity_id',
            'type'  => 'number',
        ));
        $this->addColumn('firstname', array(
            'header'    => Mage::helper('customer')->__('用户名'),
            'index'     => 'firstname'
        ));
        $this->addColumn('lastname', array(
            'header'    => Mage::helper('customer')->__('店铺名'),
            'index'     => 'lastname'
        ));
        /*$this->addColumn('name', array(
            'header'    => Mage::helper('customer')->__('Name'),
            'index'     => 'name'
        ));
        */
        // $this->addColumn('email', array(
        //     'header'    => Mage::helper('customer')->__('Email'),
        //     'width'     => '150',
        //     'index'     => 'email'
        // ));

        $groups = Mage::getResourceModel('customer/group_collection')
            ->addFieldToFilter('customer_group_id', array('gt'=> 0))
            ->load()
            ->toOptionHash();

        $this->addColumn('group', array(
            'header'    =>  Mage::helper('customer')->__('Group'),
            'width'     =>  '100',
            'index'     =>  'group_id',
            'type'      =>  'options',
            'options'   =>  $groups,
        ));

        $this->addColumn('telephone', array(
            'header'    => Mage::helper('customer')->__('Telephone'),
            'width'     => '100',
            'index'     => 'mobile'
        ));

        $this->addColumn('billing_id', array(
            'header'    => Mage::helper('customer')->__('地址'),
            'width'     => '200',
            'index'     => 'billing_id',
            'filter'    => false,
            'sortable'  => false,
            'renderer'    => 'Silk_Zgb_Block_Renderer_Address'
        ));

        // $this->addColumn('billing_country_id', array(
        //     'header'    => Mage::helper('customer')->__('Country'),
        //     'width'     => '100',
        //     'type'      => 'country',
        //     'index'     => 'billing_country_id',
        // ));

        // $this->addColumn('billing_region', array(
        //     'header'    => Mage::helper('customer')->__('State/Province'),
        //     'width'     => '100',
        //     'index'     => 'billing_region',
        // ));

        $this->addColumn('customer_since', array(
            'header'    => Mage::helper('customer')->__('Customer Since'),
            'type'      => 'datetime',
            'align'     => 'center',
            'index'     => 'created_at',
            'gmtoffset' => true
        ));

        $this->addColumn('orders', array(
             'header'    => Mage::helper('customer')->__('订单数'),
             'width'     => '80',
             'index'     => 'entity_id',
             'filter'    => false,
             'sortable'  => false,
             'renderer'    => 'Silk_Zgb_Block_Renderer_Orders'
        ));
        $this->addColumn('order_total', array(
             'header'    => Mage::helper('customer')->__('订单额'),
             'width'     => '80',
             'index'     => 'entity_id',
             'filter'    => false,
             'sortable'  => false,
             'renderer'    => 'Silk_Zgb_Block_Renderer_Spend'
        ));

        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('website_id', array(
                'header'    => Mage::helper('customer')->__('Website'),
                'align'     => 'center',
                'width'     => '80px',
                'type'      => 'options',
                'options'   => $this->getWebsite(),
                'index'     => 'website_id',
            ));
        }

        $this->addColumn('confirmation', array(
                'header'    => Mage::helper('customer')->__('审核状态'),
                'align'     => 'center',
                'width'     => '80px',
                'index'     => 'confirmation',
                'renderer'    => 'Silk_Zgb_Block_Renderer_Confimation'
            ));

        $this->addColumn('action',
            array(
                'header'    =>  Mage::helper('customer')->__('Action'),
                'width'     => '100',
                'type'      => 'action',
                'getter'    => 'getId',
                'actions'   => array(
                    array(
                        'caption'   => Mage::helper('customer')->__('Edit'),
                        'url'       => array('base'=> '*/*/edit'),
                        'field'     => 'id'
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
        ));

        $this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
        $this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
        return parent::_prepareColumns();
    }

    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('customer');

        $this->getMassactionBlock()->addItem('delete', array(
             'label'    => Mage::helper('customer')->__('Delete'),
             'url'      => $this->getUrl('*/*/massDelete'),
             'confirm'  => Mage::helper('customer')->__('Are you sure?')
        ));

        $this->getMassactionBlock()->addItem('msg', array(
             'label'    => Mage::helper('customer')->__('短信息'),
             'url'      => $this->getUrl('*/*/msg'),
        ));

        // $this->getMassactionBlock()->addItem('newsletter_subscribe', array(
        //      'label'    => Mage::helper('customer')->__('Subscribe to Newsletter'),
        //      'url'      => $this->getUrl('*/*/massSubscribe')
        // ));

        // $this->getMassactionBlock()->addItem('newsletter_unsubscribe', array(
        //      'label'    => Mage::helper('customer')->__('Unsubscribe from Newsletter'),
        //      'url'      => $this->getUrl('*/*/massUnsubscribe')
        // ));




        $groups = $this->helper('customer')->getGroups()->toOptionArray();

        array_unshift($groups, array('label'=> '', 'value'=> ''));
        $this->getMassactionBlock()->addItem('assign_group', array(
             'label'        => Mage::helper('customer')->__('Assign a Customer Group'),
             'url'          => $this->getUrl('*/*/massAssignGroup'),
             'additional'   => array(
                'visibility'    => array(
                     'name'     => 'group',
                     'type'     => 'select',
                     'class'    => 'required-entry',
                     'label'    => Mage::helper('customer')->__('Group'),
                     'values'   => $groups
                 )
            )
        ));

        return $this;
    }

    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=> true));
    }

    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array('id'=>$row->getId()));
    }
    protected function getWebsite()
    {
        $websiteId = $this->_getWebsiteId();
        $website = Mage::getModel('core/website')->getCollection()->toOptionHash();
        $outs= array();
        if (key_exists($websiteId,$website))
        {
          $websitename = $website[$websiteId];
          $outs[$websiteId]=$websitename;
          return $outs;

        }
          return $website;
    }
    protected function _getWebsiteId(){
        $websiteId = Mage::getSingleton('admin/session')->getUser()->getWebsiteId();
        return $websiteId;
    }
}
